package jpb.module.dao.member;

import jpb.module.dao.member.mapper.MemberMapper;
import jpb.module.daomain.member.LoadMem;
import jpb.module.daomain.member.Member;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by haol on 2017/4/8.
 */
@Repository
public class MemberDao {
        @Autowired
        private MemberMapper mapper;
        private Logger logger = LoggerFactory.getLogger(MemberDao.class);

        public void update(Member member) {
                try {
                        mapper.update(member);
                        this.insertMobile(member.getMobiles(), member.getMemberNum());
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }

        }

        public void complete(Member member) {
                try {
                        mapper.complete(member);
                        this.insertMobile(member.getMobiles(), member.getMemberNum());
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }


        public void updateTime(String num, String time) {
                try {
                        mapper.updateTime(num, time);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }

        }


        /**
         * 包含表member和member_log的内容
         * @param memberNum
         * @return
         */
        public Member get(String memberNum) {
                try {
                        Member member = mapper.get(memberNum);
                        if (member == null) {
                                return null;
                        }else {
                                return this.addMobiles(member);
                        }
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }

        /**
         * 包含表member的内容
         * @param memberNum
         * @return
         */
        public Member getMsg(String memberNum) {
                try {
                        Member member = mapper.getMsg(memberNum);
                        if (member == null) {
                                return null;
                        }else {
                                return this.addMobiles(member);
                        }
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }

        public Member addMobiles(Member member){
                List<String> mobiles = this.getMobiles(member.getMemberNum());
                if (mobiles != null) {
                        member.setMobiles(mobiles);
                }
                return member;
        }


        public Integer getCount(String memberNum) {
                try {
                        return mapper.getCount(memberNum);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }


        public List<Member> getDetailByOid(String oid) {
                try {
                        List<Member> members = mapper.getDetailByOid(oid);
                        return members;
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }
        public List<LoadMem> getDetailByDid(Integer did) {
                try {
                        List<LoadMem> members = mapper.getDetailByDid(did);
                        return members;
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }
        public List<Member> getByOid(String oid) {
                try {
                        return mapper.getByOid(oid);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }

        public void insertMobile(List<String> mobiles, String num) {
                try {
                        this.deleteMobile(num);
                        for (String mo : mobiles) {
                                mapper.insertMobile(num, mo);
                        }
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }

        public void deleteMobile(String num) {
                try {
                        mapper.deleteMobile(num);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }

        public List<String> getMobiles(String num) {
                try {
                        return mapper.getMobiles(num);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }


        public void delete(String num) {
                try {
                        mapper.delete(num);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                }
        }



        public List<Member> getFollowByOid(String oid) {
                try {
                       return mapper.getFollowByOid(oid);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        return null;
                }
        }
        public List<Member> getByDid(Integer id){
                try {
                        return mapper.getByDid(id);
                }catch (Exception e){
                        logger.error(e.getMessage());
                        return null;
                }
        }


}
